package com.sy.controller;

import com.sy.dto.JsonResult;
import com.sy.entity.Cartoon;
import com.sy.service.CartoonService;
import com.sy.utils.RandomUtil2;
import com.sy.vo.CartoonVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("cartoon")
public class CartoonController {

    @Autowired
    private CartoonService service;

    /**
     * 首页随机加载10个漫画展示
     */
    @RequestMapping("getTen")
    public JsonResult getTenCartoons() {
        JsonResult result = new JsonResult();
        Set<Integer> ten = RandomUtil2.getTen();
        List<Cartoon> cartoons = service.selectCartoonTen(ten);
//        System.out.println(cartoons.size());
        result.setData(cartoons);
        return result;
    }

    /**
     * 首页加载5个冒险漫画展示
     */
    @RequestMapping("Adventure")
    public JsonResult getAdventureCartoon() {
        JsonResult result = new JsonResult();
        List<Cartoon> adventureCartoon = service.getAdventureCartoon();
        result.setData(adventureCartoon);
        return result;
    }


    /**
     * 首页加载5个热血漫画展示
     */
    @RequestMapping("Passionate")
    public JsonResult getPassionateCartoon() {
        JsonResult result = new JsonResult();
        List<Cartoon> passionateCartoon = service.getPassionateCartoon();
        result.setData(passionateCartoon);
        return result;
    }

    /**
     * 根据id查询漫画详细信息
     */
    @RequestMapping(value = "{cid}", method = RequestMethod.GET)
    public JsonResult getCartoonById(@PathVariable("cid") Integer cid) {
        JsonResult result = new JsonResult();
        Cartoon cartoonById = service.getCartoonById(cid);
        int i = service.updateClickNumber(cid);
        result.setData(cartoonById);
        return result;
    }

    /**
     * 分类动态查找漫画
     */
    @RequestMapping
    public JsonResult getClassify(CartoonVo vo){

        JsonResult result = new JsonResult();
        if (null!=vo.getcSort()){
            if (1==vo.getcSort()){
                vo.setSort("updateTime");
            }
            if (2==vo.getcSort()){
                vo.setSort("numberOfPeople");
            }
            if (3==vo.getcSort()){
                vo.setSort("launchTime");
            }
        }
        System.out.println(vo);
        List<Cartoon> cartoons = service.classifySelect(vo);
        if (CollectionUtils.isEmpty(cartoons)){
            result.setCode(404);
            result.setErrMsg("什么都没有");
        }else {
            result.setCode(200);
            result.setData(cartoons);
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.GET,params = "user")
    public JsonResult getSortByMonthly(HttpServletRequest req){
        JsonResult result = new JsonResult();
        String user = req.getParameter("user");
        System.out.println("==>:"+user);
        List<Cartoon> search = service.getSearch(user);
        result.setData(search);
        return result;
    }


}
